package common

import (
	"backend/service/redis"
	"github.com/beego/beego/v2/server/web/context"
)

var Redis redis.MyRedis


// SaveToken
// 存储token
func SaveToken(token string,userId string){
	Redis.Set("token:"+token, userId, 24*60*60)
}

// CheckToken
// 从redis中检测token是否有效
func CheckToken(token string) (bool, string) {
	value := Redis.Get("token:" + token)
	if value != "" {
		return true, value
	} else {
		return false, ""
	}
}

// SaveAdminToken
// 存储token
func SaveAdminToken(token string,userId string){
	Redis.Set("admin-token:"+token, userId, 24*60*60)
}

// CheckAdminToken
// 从redis中检测token是否有效
func CheckAdminToken(token string) (bool, string) {
	value := Redis.Get("admin-token:" + token)
	if value != "" {
		return true, value
	} else {
		return false, ""
	}
}

// GetUserId
// 通过controller的request header中的token来获取redis中的token
func GetUserId(c *context.Context) string {
	token := c.Input.Header("Token")
	_, value := CheckToken(token)
	return value
}

func GetAdminId(c *context.Context)string {
	token := c.Input.Header("Token")
	if token ==""{
		token = c.GetCookie("token")
	}
	_, value := CheckAdminToken(token)
	return value
}